package com.youkeyi.ddy.cloud.basic.pub.annex.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youkeyi.ddy.cloud.basic.pub.annex.entity.SysAnnex;
import com.youkeyi.ddy.cloud.basic.pub.annex.entity.SysAnnexType;
import com.youkeyi.ddy.cloud.basic.pub.annex.service.SysAnnexService;
import com.youkeyi.ddy.cloud.basic.pub.annex.service.SysAnnexTypeService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.PublicAnnexCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.dto.PublicAnnexListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.dto.PublicAnnexListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.param.PublicAnnexCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.param.PublicAnnexDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.annex.param.PublicAnnexQueryCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicPublicConstant;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SimpleUUidUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 附件上传信息 前端控制器
 * </p>
 *
 * @annexor xiawang.liu
 * @since 2024-02-29
 */
@Service
public class SysAnnexController implements PublicAnnexCloudService {
    
    @Autowired
    private SysAnnexService sysAnnexService;
    @Autowired
    private SysAnnexTypeService sysAnnexTypeService;

    @Override
    public PublicAnnexListCloudDto query(PublicAnnexQueryCloudParam param) {
        PublicAnnexListCloudDto dto = new PublicAnnexListCloudDto();
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        if (CollectionUtils.isNotEmpty(param.getAnnexPrimaryKeys())) {
            param.getAnnexPrimaryKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorpKeys())) {
            param.getCorpKeys().removeAll(Collections.singleton(null));
        }
        if (CollectionUtils.isNotEmpty(param.getCorrelationKeys())) {
            param.getCorrelationKeys().removeAll(Collections.singleton(null));
        }

        QueryWrapper<SysAnnex> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .in(CollectionUtils.isNotEmpty(param.getAnnexPrimaryKeys()), SysAnnex::getPrimaryKey, param.getAnnexPrimaryKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorpKeys()), SysAnnex::getCorpKey, param.getCorpKeys())
                .in(CollectionUtils.isNotEmpty(param.getCorrelationKeys()), SysAnnex::getCorrelationKey, param.getCorrelationKeys())
                .like(StringUtils.isNotBlank(param.getFileNameFuzzy()), SysAnnex::getFileName, param.getFileNameFuzzy())
                .ne(SysAnnex::getIsDeleted, YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value())
        ;

        Page<SysAnnex> page = new Page<>();
        if (null != param.getCurrentPage() && null != param.getPageSize()) {
            page.setCurrent(param.getCurrentPage());
            page.setSize(param.getPageSize());
        } else {
            page.setCurrent(1);
            page.setSize(99999999);
        }
        Page<SysAnnex> annexList = sysAnnexService.page(page, wrapper);
        if (CollectionUtils.isNotEmpty(annexList.getRecords())) {
            dto.setTotal(annexList.getTotal());

            List<String> annexTypeKeys = annexList.getRecords().stream().map(SysAnnex::getAnnexTypeKey).distinct().collect(Collectors.toList());
            Map<String, SysAnnexType> typeMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(annexTypeKeys)) {
                QueryWrapper<SysAnnexType> typeWrapper = new QueryWrapper<>();
                typeWrapper.lambda().in(SysAnnexType::getPrimaryKey, annexTypeKeys);
                List<SysAnnexType> sysAnnexTypes = sysAnnexTypeService.list(typeWrapper);
                if (CollectionUtils.isNotEmpty(sysAnnexTypes)) {
                    typeMap.putAll(sysAnnexTypes.stream().collect(Collectors.toMap(SysAnnexType::getPrimaryKey, Function.identity(), (k1, k2) -> k1)));
                }
            }

            List<PublicAnnexListItemCloudDto> items = new ArrayList<>();
            annexList.getRecords().forEach(annex -> {
                PublicAnnexListItemCloudDto item = new PublicAnnexListItemCloudDto();
                item.setAnnexPrimaryKey(annex.getPrimaryKey());
                item.setCorpKey(annex.getCorpKey());
                item.setCorrelationKey(annex.getCorrelationKey());
                item.setFileName(annex.getFileName());
                item.setUrl(annex.getUrl());
                item.setAnnexTypePrimaryKey(annex.getAnnexTypeKey());
                if (typeMap.containsKey(annex.getAnnexTypeKey())) {
                    SysAnnexType annexType = typeMap.get(annex.getAnnexTypeKey());
                    item.setAnnexTypeCode(annexType.getCode());
                    item.setAnnexTypeName(annexType.getName());
                }
                item.setIsDeleted(annex.getIsDeleted());
                item.setIsDeletedName(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO.getName(item.getIsDeleted()));
                item.setCreateTime(annex.getCreateTime() != null ? annex.getCreateTime().getTime() : null);
                item.setCreateUserKey(annex.getCreateUserKey());
                item.setCreateUserName(annex.getCreateUserName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(PublicAnnexCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || StringUtils.isBlank(param.getFileName()) || StringUtils.isBlank(param.getUrl())
                || StringUtils.isBlank(param.getCorpKey())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        SysAnnex annex = new SysAnnex();
        annex.setPrimaryKey(SimpleUUidUtils.getUuid());
        annex.setCorpKey(param.getCorpKey());
        annex.setCorrelationKey(param.getCorrelationKey());
        annex.setFileName(param.getFileName());
        annex.setUrl(param.getUrl());
        annex.setAnnexTypeKey(param.getAnnexTypeKey());
        annex.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_NO.value());
        annex.setCreateUserKey(param.getCreateUserKey());
        annex.setCreateUserName(param.getCreateUserName());
        annex.setCreateTime(new Date());
        boolean save = sysAnnexService.save(annex);
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
            return rc;
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        rc.setKey(annex.getPrimaryKey());
        return rc;
    }

    @Override
    public ResponseContent delete(PublicAnnexDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        if (null == param || CollectionUtils.isEmpty(param.getAnnexPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }
        UpdateWrapper<SysAnnex> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .in(SysAnnex::getPrimaryKey, param.getAnnexPrimaryKeys());

        SysAnnex annex = new SysAnnex();
        annex.setIsDeleted(YoukeyiDdyCloudBasicPublicConstant.SYSTEM_YES_OR_NO_YES.value());
        annex.setUpdateUserKey(param.getUpdateUserKey());
        annex.setUpdateUserName(param.getUpdateUserName());
        annex.setUpdateTime(new Date());
        boolean save = sysAnnexService.update(annex, wrapper);
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        if (!save) {
            rc.setErrorMessage(ResponseCodeEnum.FAIL.getMessage());
        }
        return rc;
    }
}
